package com.cgbl.cg.im.viewholder;

import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;

import com.cgbl.cg.databinding.AppItemMessageBinding;
import com.netease.yunxin.kit.common.ui.utils.AvatarColor;
import com.netease.yunxin.kit.common.ui.utils.TimeFormatUtils;
import com.netease.yunxin.kit.common.ui.viewholder.BaseViewHolder;
import com.netease.yunxin.kit.conversationkit.ui.ConversationKitClient;
import com.netease.yunxin.kit.conversationkit.ui.ConversationUIConfig;
import com.netease.yunxin.kit.conversationkit.ui.common.ConversationUtils;
import com.netease.yunxin.kit.conversationkit.ui.model.ConversationBean;

public class CustomP2PVIewHolder extends BaseViewHolder<ConversationBean> {

    private AppItemMessageBinding viewBinding;
    private Drawable stickTopDrawable;
    private Drawable itemDrawable;

    public CustomP2PVIewHolder(@NonNull AppItemMessageBinding binding) {
        super(binding.getRoot());
        viewBinding = binding;
    }

    @Override
    public void onBindData(ConversationBean data, int position) {
        loadUIConfig();
        viewBinding.ivAvatar.setData(data.infoData.getAvatar(), data.infoData.getName(), AvatarColor.avatarColor(data.infoData.getContactId()));
        viewBinding.conversationNameTv.setText(data.infoData.getName());
        if (data.infoData.isStickTop()) {
            viewBinding.rootView.setBackground(stickTopDrawable);
        } else {
            viewBinding.rootView.setBackground(itemDrawable);
        }
        if (data.infoData.getMute()) {
            viewBinding.conversationMuteIv.setVisibility(View.VISIBLE);
            viewBinding.conversationUnreadTv.setVisibility(View.GONE);
        } else {
            viewBinding.conversationMuteIv.setVisibility(View.GONE);
            if (data.infoData.getUnreadCount() > 0) {
                int count = data.infoData.getUnreadCount();
                String content;
                if (count >= 100) {
                    content = "99+";
                } else {
                    content = String.valueOf(count);
                }
                viewBinding.conversationUnreadTv.setText(content);
                viewBinding.conversationUnreadTv.setVisibility(View.VISIBLE);
            } else {
                viewBinding.conversationUnreadTv.setVisibility(View.GONE);
            }
        }
        viewBinding.conversationMessageTv.setText(ConversationUtils.getConversationText(itemView.getContext(), data.infoData));
        viewBinding.conversationTime.setText(TimeFormatUtils.formatMillisecond(viewBinding.getRoot().getContext(), data.infoData.getTime()));
        viewBinding.conversationTimeFl.setOnClickListener(v -> itemListener.onClick(data, position));
        viewBinding.conversationTimeFl.setOnLongClickListener(
                v -> itemListener.onLongClick(data, position));
//        viewBinding.conversation/AvatarFl.setOnClickListener(
//                v -> itemListener.onAvatarClick(data, position));
//        viewBinding.conversationAvatarFl.setOnLongClickListener(
//                v -> itemListener.onAvatarLongClick(data, position));
    }

    private void loadUIConfig() {
        if (ConversationKitClient.getConversationUIConfig() != null) {
            ConversationUIConfig config = ConversationKitClient.getConversationUIConfig();
            if (config.itemTitleColor != ConversationUIConfig.INT_DEFAULT_NULL) {
                viewBinding.conversationNameTv.setTextColor(config.itemTitleColor);
            }
            if (config.itemTitleSize != ConversationUIConfig.INT_DEFAULT_NULL) {
                viewBinding.conversationNameTv.setTextSize(config.itemTitleSize);
            }

            if (config.itemContentColor != ConversationUIConfig.INT_DEFAULT_NULL) {
                viewBinding.conversationMessageTv.setTextColor(config.itemContentColor);
            }
            if (config.itemContentSize != ConversationUIConfig.INT_DEFAULT_NULL) {
                viewBinding.conversationMessageTv.setTextSize(config.itemContentSize);
            }

            if (config.itemDateColor != ConversationUIConfig.INT_DEFAULT_NULL) {
                viewBinding.conversationTime.setTextColor(config.itemDateColor);
            }
            if (config.itemDateSize != ConversationUIConfig.INT_DEFAULT_NULL) {
                viewBinding.conversationTime.setTextSize(config.itemDateSize);
            }

            if (config.avatarCornerRadius != ConversationUIConfig.INT_DEFAULT_NULL) {
                viewBinding.ivAvatar.setCornerRadius(config.avatarCornerRadius);
            }
            if (config.itemBackground != null) {
                itemDrawable = config.itemBackground;
            }
            if (config.itemStickTopBackground != null) {
                stickTopDrawable = config.itemStickTopBackground;
            }
        } else {
            itemDrawable = viewBinding.getRoot().getContext().getDrawable(com.netease.yunxin.kit.conversationkit.ui.R.drawable.view_normal_selector);
            stickTopDrawable = viewBinding.getRoot().getContext().getDrawable(com.netease.yunxin.kit.conversationkit.ui.R.drawable.view_select_selector);
        }
    }
}
